package com.alinesno.cloud.base.logger.alarm;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alinesno.cloud.base.logger.alarm.constants.AlarmTypeConstants;
import com.alinesno.cloud.base.logger.entity.LogTriggerEntity;
import com.alinesno.cloud.base.logger.service.ILogTriggerService;
import com.alinesno.cloud.common.core.context.ApplicationContextProvider;
import com.alinesno.cloud.common.facade.enums.HasStatusEnums;

/**
 * 定时任务
 * @author WeiXiaoJin
 * @since 2020年1月29日 下午19:45:08
 */
@Component
public class AlarmJob {
	
	private static final Logger log = LoggerFactory.getLogger(AlarmJob.class) ; 

	@Autowired
	private ILogTriggerService logTriggerService ; 

	/**
	 * 预警功能 
	 */
	private AlarmService alarmService = null ; 
	
	/**
	 * 定时任务(每一分钟执行一次)
	 */
	@Scheduled(cron = "0 0/1 * * * ?")
	public void doJob() {
		
		log.debug(">>>>>>>>>>>>>>  执行预警定时任务  >>>>>>>>>>>>>>>>");
		
		List<LogTriggerEntity> list = logTriggerService.findAllByHasStatus(HasStatusEnums.LEGAL.value) ; 
		for(LogTriggerEntity t : list) {
			if(StringUtils.isNoneBlank(t.getAlarmEventType())) {
				alarmService = ApplicationContextProvider.getBean(t.getAlarmEventType()+AlarmTypeConstants.SERVICE_SUFFIX , AlarmService.class) ; 
				if(alarmService != null) {
					try {
						alarmService.alarm(t); ; 
					}catch(Exception e) {
						log.error("调用异常:" , e) ; 
					}
				}
			}
		}
		
	}
	
}
